package com.trkj.service.cf;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.cf.TraininggactivityDAO;
import com.trkj.pojo.Organization;
import com.trkj.pojo.OrganizationStructure;
import com.trkj.pojo.Traininggactivity;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor=Exception.class)
public class TraininggactivityService {
	@Autowired
	private TraininggactivityDAO traininggactivityDAO;
	/**
	 * 培训活动
	 * @return
	 */
	public long pageCount() {
		 
		return traininggactivityDAO.count();
	}
	//查询分页
	public List<Traininggactivity> userPage(Integer page, Integer rows)throws ServiceException {
		try {
			List<Traininggactivity> list = traininggactivityDAO.queryByPage(new PageRequest(page - 1, rows));
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	// 搜索
		public List<Traininggactivity> filedByName(String trainingName) throws ServiceException {
			try {
				return traininggactivityDAO.queryBytrainingName("%" + trainingName + "%");
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		}
		// 新增
		public void traininggactivityadd(String trainingName, Timestamp trainingStarttime, Timestamp trainingEndtime
				,String trainingLocation,String certificateRemarks) throws ServiceException {
			// Object sessions= session.getAttribute("staff");
			try {
				System.out.println(trainingName+trainingStarttime+trainingEndtime+trainingLocation+certificateRemarks+"j");
				Date date = new Date();   
				Traininggactivity o = new Traininggactivity();
				o.setTrainingName(trainingName);
				o.setTrainingStarttime(new Timestamp(date.getTime()));
				//o.setTrainingEndtime(trainingEndtime);
				o.setTrainingLocation(trainingLocation);
				o.setCertificateRemarks(certificateRemarks);
				traininggactivityDAO.save(o);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		}
		//删除
		public void definedutyremove(Long trainingId) {
			traininggactivityDAO.delete(trainingId);
		}
		// 编辑
		public void traininggactivityupdate(Long trainingId, String trainingName, String trainingLocation,String certificateRemarks) throws ServiceException {
			try {
				Traininggactivity or = traininggactivityDAO.findOne(trainingId);
				Date date = new Date();   
				 or.setTrainingName(trainingName);
				// or.setTrainingStarttime(new Timestamp(date.getTime()));
				 or.setTrainingEndtime(new Timestamp(date.getTime()));
				 or.setTrainingLocation(trainingLocation);
				 or.setCertificateRemarks(certificateRemarks);
				traininggactivityDAO.save(or);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		}
		 

	
}
